package com.yunshipei.wps;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

import cn.wps.moffice.client.ActionType;
import cn.wps.moffice.client.AllowChangeCallBack;
import cn.wps.moffice.client.OfficeEventListener;
import cn.wps.moffice.client.OfficeInputStream;
import cn.wps.moffice.client.OfficeOutputStream;
import cn.wps.moffice.client.ViewType;
import cn.wps.moffice.service.doc.Document;

public class OfficeEventListenerImpl extends OfficeEventListener.Stub {
    protected MOfficeClientService service = null;
    private Document mDoc;

    private boolean mIsValidPackage = true;

    public OfficeEventListenerImpl(MOfficeClientService service) {
        this.service = service;
    }

    @Override
    public int onOpenFile(String path, OfficeOutputStream output) throws RemoteException {
        Log.d("OfficeEventListener", "onOpenFile");

        if (!mIsValidPackage)
            return -1;

        return EncryptClass.encryptOpenFile(path, output);

    }

    @Override
    public int onSaveFile(OfficeInputStream input, String path) throws RemoteException {
        Log.d("OfficeEventListener", "onSaveFile");

        SettingPreference settingPreference;
        settingPreference = new SettingPreference(this.service.getApplicationContext());
        boolean isEncrypt = settingPreference.getSettingParam(Define.ENCRYPT_FILE, true);

        if (isEncrypt)
            return EncryptClass.encryptSaveFile(input, path);
        else
            return EncryptClass.normalSaveFile(input, path);
    }

    @Override
    public int onCloseFile() throws RemoteException {
        Log.d("OfficeEventListener", "onCloseFile");
        return 0;
    }

    @Override
    public boolean isActionAllowed(String path, ActionType type) throws RemoteException {
        Log.d("OfficeEventListener", "isActionAllowed" + type.toString());

        SettingPreference settingPreference;
        settingPreference = new SettingPreference(this.service.getApplicationContext());

        if (type.equals(ActionType.AT_EDIT_REVISION)) {
            return isRevisionMode(path, type, settingPreference); //如果是接受或拒绝某条修订的事件,做特殊处理
        }

//		if (type.equals(ActionType.AT_SHARE)) {
//			return false;
//		}

        boolean result = true;
        boolean typeAT = settingPreference.getSettingParam(type.toString(), true);
        String pathAT = settingPreference.getSettingParam(Define.AT_PATH, "/");
        boolean isExist = path.startsWith(pathAT) || path.equals("");  //有部分事件传过来路径为"",
        if (!typeAT && isExist)
            result = false;

        return result;
    }

    @Override
    public boolean isViewForbidden(String arg0, ViewType arg1) throws RemoteException {
        if (arg1 == ViewType.VT_FILE_PRINT) {
            return false;
        } else if (arg1 == ViewType.VT_FILE_SAVE) {
            return false;
        }
        return false;
    }

    @Override
    public boolean isViewInVisible(String arg0, ViewType arg1) throws RemoteException {
//		if (arg1 == ViewType.VT_MENU_REVIEW || arg1 == ViewType.VT_MENU_PEN)
//		{
//			return true;
//		}
        return false;
    }

    @Override
    public void onMenuAtion(String path, String id) throws RemoteException {
        //TODO　自定义菜单，path为文档路径，id为传入的菜单按钮id
        mDoc = service.mService.getActiveDocument();
        if (mDoc == null) {
            Util.showToast(this.service.getApplicationContext(), "服务已经断开，请重启程序");
        } else {
            FileOperations fOperations = new FileOperations(mDoc, id);
            fOperations.operation();
        }
    }

    @Override
    public String getMenuText(String path, String id) throws RemoteException {
        switch (id) {
            case "menu_id_comment":
                if (mDoc.isInRevisionMode()) {
                    return "退出修订";
                } else {
                    return "进入修订";
                }
            case "menu_id_protect":
                if (mDoc.isProtectOn()) {
                    return "关闭保护";
                } else {
                    return "只读保护";
                }
            default:
                break;
        }
        return null;
    }

    //是否可以操作他人修订（作者名不同的修订）
    private boolean isRevisionMode(String path, ActionType type, SettingPreference settingPreference) {
        String docUserName = settingPreference.getSettingParam(Define.USER_NAME, "");
        boolean typeAT = settingPreference.getSettingParam(type.toString(), true);
        boolean isSameOne = docUserName.equals(path);    //在此事件中，path中存放是是作者批注名
        if (!typeAT && !isSameOne) {
            return false;
        }

        return true;
    }

    /**
     * 实现多个可变包名的验证
     * originalPackage是最原始的第三方包名，华为渠道为“com.huawei.svn.hiwork”
     * thirdPackage为可变动的应用包名，具体有企业资金定制
     */
    @Override
    public boolean isValidPackage(String originalPackage, String thirdPackage) throws RemoteException {
        //此处是某些企业的特殊需求，可以忽略，直接返回false即可
        return false;
    }

    @Override
    public void setAllowChangeCallBack(AllowChangeCallBack allowChangeCallBack) throws RemoteException {

    }

    @Override
    public int invoke(String actionID, String args) throws RemoteException {
        if ("addPicture".equals(actionID)) {
//			OfficeService officeService = service.getOfficeService();
//			officeService.getActiveDocument().getShapes().addPicture(args, false, false, 100, 100, 100, 100, 0, WrapType.None);
        } else if ("showDialog".equals(actionID)) {
            Message msg = Message.obtain();
            showDialogHandler.sendMessage(msg);
        }
        return 0;
    }

    private Handler showDialogHandler = new Handler() {
        public void handleMessage(Message msg) {
            showSystemDialog(OfficeEventListenerImpl.this.service.getApplicationContext());
        }
    };

    private void showSystemDialog(Context context) {
        TextView t = new TextView(context);
        t.setText("showSystemDialog");
        AlertDialog d = new AlertDialog.Builder(context).create();
        d.setView(t, 0, 0, 0, 0);
        d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        d.show();
    }
}
